package com.senac.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.StringUtils;
import com.senac.exception.GerenciamentoEscolaException;
import com.senac.exception.GerenciamentoEscolaException.ERROR;
import com.senac.service.GerenciamentoEscolaService;

public abstract class GerenciamentoEscolaAvancado<E> extends GerenciamentoEscolaServlet<E> {

	
	private static final long serialVersionUID = 5949441498801946108L;

	protected abstract String getUrlListagem();
	
	protected abstract String getUrlCadastro();
	
	protected abstract String getPageView();
	
	protected abstract String getPageList();
	
	protected abstract GerenciamentoEscolaService<E> getService();
	
	protected abstract String getViewAttributeName();
	
	protected abstract String getListAttributeName();
	
	protected void cadastrar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		req.getRequestDispatcher(getPageView()).forward(req, resp);
	}
	
	protected void salvar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {		
		String idString = req.getParameter("id");
		try {
			GerenciamentoEscolaService<E> service = getService();
			E entity = mountByRequest(req);
			boolean ok = service.save(entity);
			if(ok) {
				resp.sendRedirect(getUrlListagem()+"?msg=salvo");
			} else {
				
				resp.sendRedirect(getUrlListagem()+"?msg=erro_salvar");
			}			
		} catch (GerenciamentoEscolaException e) {
			ERROR error = e.getError();
			if(StringUtils.isNullOrEmpty(idString)) {
				req.getRequestDispatcher(getUrlCadastro()+"?acao=&msg="+error.toString().toLowerCase()).forward(req, resp);
			} else {
				req.getRequestDispatcher(getUrlCadastro()+"?acao=editar&id="+idString+"&msg="+error.toString().toLowerCase()).forward(req, resp);
			}
		} catch (Exception e) {
			resp.sendRedirect(getUrlListagem()+"?msg=erro_salvar");
		}
		
	}
	
	protected void editar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		GerenciamentoEscolaService<E> service = getService();
		String idString = req.getParameter("id");
		Integer id = Integer.parseInt(idString);
		E entity = service.getById(id);
		req.setAttribute(getViewAttributeName(), entity);
		String url = getPageView() + "?" + req.getQueryString();
		req.getRequestDispatcher(url).forward(req, resp);
	}
	
	protected void excluir(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		GerenciamentoEscolaService<E> service = getService();
		String idString = req.getParameter("id");
		Integer id = Integer.parseInt(idString);
		boolean ok = service.delete(id);
		if(ok) {
			resp.sendRedirect(getUrlListagem()+"?msg=removido");
		} else {
			resp.sendRedirect(getUrlListagem()+"?msg=erro_remover");
		}
	}
	
	protected void listar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		GerenciamentoEscolaService<E> service = getService();
		
		List<E> list = service.get();
				
		req.setAttribute(getListAttributeName(), list);
		req.getRequestDispatcher(getPageList()).forward(req, resp);
	}
	

	
	
}
